//
//  BCProjectDetailProjectDataCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailProjectDataCell.h"

#import "BCProjectDetailDataView.h"

#import <OCSkeleton.h>

#import "BCProjectDetailResponse.h"

static CGFloat BCProjectDetailProjectDataCellTopHeight = 120.0;

static CGFloat BCProjectDetailProjectDataCellLeftPadding = 33.0;

@interface BCProjectDetailProjectDataCell () <OCGradientsOwner>

@property (nonatomic, strong) UIView *topContentView;

/**
 *lastDataLabel
 */
@property (nonatomic, strong) UILabel *lastDataLabel;
/**
 *lastCaptionLabel
 */
@property (nonatomic, strong) UILabel *lastCaptionLabel;
/**
 *rangeLabel
 */
@property (nonatomic, strong) UILabel *rangeLabel;

/**
 *BCProjectDetailDataView
 */
@property (nonatomic, strong) BCProjectDetailDataView *dataView;


#pragma mark - ContainerViews
@property (nonatomic, strong) OCGradientContainerView *lastDataContainerView;
@property (nonatomic, strong) OCGradientContainerView *lastCaptionContainerView;
@property (nonatomic, strong) OCGradientContainerView *rangeContainerView;
@property (nonatomic, strong) OCGradientContainerView *rateTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *rateDataContainerView;
@property (nonatomic, strong) OCGradientContainerView *goalTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *goalDataContainerView;

/**
 *gradientLayers
 */
@property (nonatomic, strong) NSMutableArray *oc_gradientLayers;
/**
 *skeletonViews
 */
@property (nonatomic, strong) NSMutableArray *oc_skeletonViews;

@end


@implementation BCProjectDetailProjectDataCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}
- (void)setupSubviews {
    self.topContentView = [UIView new];
    [self.contentView addSubview:self.topContentView];
    [self.topContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(BCProjectDetailProjectDataCellTopHeight);
    }];
    //
    self.lastDataLabel = [UILabel new];
    self.lastDataLabel.textColor = [UIColor whiteColor];
    self.lastDataLabel.adjustsFontSizeToFitWidth = true;
    self.lastDataLabel.font = [UIFont systemFontOfSize:30 weight:UIFontWeightMedium];
    [self.topContentView addSubview:self.lastDataLabel];
    [self.lastDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topContentView).offset(BCProjectDetailProjectDataCellLeftPadding);
        make.right.mas_equalTo(self.topContentView).offset(-BCProjectDetailProjectDataCellLeftPadding);
        make.centerY.mas_equalTo(self.topContentView).offset(-5.0);
        make.height.mas_equalTo(self.lastDataLabel.font.lineHeight+5.0);
    }];
    self.lastCaptionLabel = [UILabel new];
    self.lastCaptionLabel.textColor = [UIColor whiteColor];
    self.lastCaptionLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
    self.lastCaptionLabel.adjustsFontSizeToFitWidth = true;
    [self.topContentView addSubview:self.lastCaptionLabel];
    [self.lastCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.lastDataLabel);
        make.bottom.mas_equalTo(self.lastDataLabel.mas_top).offset(-0.0);
    }];
    self.rangeLabel = [UILabel new];
    self.rangeLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.rangeLabel.textColor = [UIColor whiteColor];
    self.rangeLabel.numberOfLines = 0;
    [self.topContentView addSubview:self.rangeLabel];
    [self.rangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.lastDataLabel);
        make.top.mas_equalTo(self.lastDataLabel.mas_bottom).offset(3.0);
    }];
    //
    self.dataView = [BCProjectDetailDataView new];
    [self.contentView addSubview:self.dataView];
    [self.dataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topContentView.mas_bottom);
        make.left.mas_equalTo(self.contentView).offset(BCProjectDetailProjectDataCellLeftPadding);
        make.right.mas_equalTo(self.topContentView).offset(-BCProjectDetailProjectDataCellLeftPadding);
        make.height.mas_equalTo([BCProjectDetailDataView defaultHeight]);
    }];
}

- (void)setContainerViews {
    self.lastDataContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.lastDataContainerView];
    [self.lastDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastDataLabel);
        make.centerY.mas_equalTo(self.lastDataLabel);
        make.height.mas_equalTo(self.lastDataLabel.font.lineHeight-10.0);
        make.width.mas_equalTo(self.lastDataLabel).multipliedBy(.5);
    }];
    [self.oc_skeletonViews addObject:self.lastDataContainerView];
    [self.oc_gradientLayers addObject:self.lastDataContainerView.gradientLayer];
    //
    self.lastCaptionContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.lastCaptionContainerView];
    [self.lastCaptionContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.lastCaptionLabel);
        make.left.mas_equalTo(self.lastCaptionLabel);
        make.height.mas_equalTo(self.lastCaptionLabel.font.lineHeight);
        make.width.mas_equalTo(self.lastCaptionLabel).multipliedBy(.25);
    }];
    [self.oc_skeletonViews addObject:self.lastCaptionContainerView];
    [self.oc_gradientLayers addObject:self.lastCaptionContainerView.gradientLayer];
    //
    self.rangeContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.rangeContainerView];
    [self.rangeContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rangeLabel);
        make.top.mas_equalTo(self.rangeLabel);
        make.width.mas_equalTo(self.rangeLabel).multipliedBy(.75);
        make.height.mas_equalTo(self.rangeLabel.font.lineHeight);
    }];
    [self.oc_skeletonViews addObject:self.rangeContainerView];
    [self.oc_gradientLayers addObject:self.rangeContainerView.gradientLayer];
    //
    self.goalTitleContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.goalTitleContainerView];
    [self.goalTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dataView.goalView.titleLabel);
        make.centerY.mas_equalTo(self.dataView.goalView.titleLabel);
        make.height.mas_equalTo(self.dataView.goalView.titleLabel);
        make.width.mas_equalTo(self.dataView.goalView.titleLabel).multipliedBy(.5);
    }];
    [self.oc_skeletonViews addObject:self.goalTitleContainerView];
    [self.oc_gradientLayers addObject:self.goalTitleContainerView.gradientLayer];
    //
    self.goalDataContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.goalDataContainerView];
    [self.goalDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dataView.goalView.dataLabel);
        make.centerY.mas_equalTo(self.dataView.goalView.dataLabel);
        make.height.mas_equalTo(self.dataView.goalView.dataLabel);
        make.width.mas_equalTo(self.dataView.goalView.titleLabel).multipliedBy(.8);
    }];
    [self.oc_skeletonViews addObject:self.goalDataContainerView];
    [self.oc_gradientLayers addObject:self.goalDataContainerView.gradientLayer];
    //
    self.rateTitleContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.rateTitleContainerView];
    [self.rateTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dataView.rateView.titleLabel);
        make.centerY.mas_equalTo(self.dataView.rateView.titleLabel);
        make.height.mas_equalTo(self.dataView.rateView.titleLabel);
        make.width.mas_equalTo(self.dataView.rateView.titleLabel).multipliedBy(.5);
    }];
    [self.oc_skeletonViews addObject:self.rateTitleContainerView];
    [self.oc_gradientLayers addObject:self.rateTitleContainerView.gradientLayer];
    //
    self.rateDataContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.rateDataContainerView];
    [self.rateDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.dataView.rateView.dataLabel);
        make.centerY.mas_equalTo(self.dataView.rateView.dataLabel);
        make.height.mas_equalTo(self.dataView.rateView.dataLabel);
        make.width.mas_equalTo(self.dataView.rateView.titleLabel).multipliedBy(.8);
    }];
    [self.oc_skeletonViews addObject:self.rateDataContainerView];
    [self.oc_gradientLayers addObject:self.rateDataContainerView.gradientLayer];
}



- (void)setCellData:(BCProjectDetailData *)cellData {
    _cellData = cellData;
    //
    if (self.cellData) {
        [self clearContainerViews];
        //eg:BTC
        NSString *curr = cellData.data.curr;
        //起购范围
        NSString *range = cellData.flow.range;
        
        self.lastDataLabel.text = cellData.data.surplus;
        self.lastCaptionLabel.text = [cellData.flow.surplus stringByReplacingOccurrencesOfString:@"{}" withString:curr];
        self.rangeLabel.text = [NSString stringWithFormat:@"%@：%@%@ - %@%@",range,cellData.data.min_buy,curr,cellData.data.max_buy,curr];
        //
        self.dataView.goalView.title = [cellData.flow.target stringByReplacingOccurrencesOfString:@"%s" withString:curr];
        self.dataView.goalView.data = cellData.data.amount;
        self.dataView.rateView.title = cellData.flow.repay;
        self.dataView.rateView.data = cellData.data.repay;
    }else {
        self.lastDataLabel.text = nil;
        self.lastCaptionLabel.text = nil;
        self.rangeLabel.text = nil;
        //
        self.dataView.goalView.title = nil;
        self.dataView.goalView.data = nil;
        self.dataView.rateView.title = nil;
        self.dataView.rateView.data = nil;
        //
        [self showContainerViews];
    }
}


- (NSMutableArray *)oc_gradientLayers {
    if (!_oc_gradientLayers) {
        _oc_gradientLayers = [NSMutableArray array];
    }
    return _oc_gradientLayers;
}
- (NSMutableArray *)oc_skeletonViews {
    if (!_oc_skeletonViews) {
        _oc_skeletonViews = [NSMutableArray array];
    }
    return _oc_skeletonViews;
}
#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    return  self.oc_gradientLayers;
}
- (NSArray <UIView *>*)skeletonViews {
    return self.oc_skeletonViews;
}

//
- (void)clearContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.alpha = 0;
    }
}
- (void)showContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.alpha = 1;
    }
}


+ (CGFloat)defaultHeight {
    return BCProjectDetailProjectDataCellTopHeight+[BCProjectDetailDataView defaultHeight]+10.0;
}

@end
